package ru.susu.algebra.chartable.writer;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

import ru.susu.algebra.chartable.constructor.SymmetricGroupMapTableConstructor;
import ru.susu.algebra.partition.Partition;
import ru.susu.algebra.partition.PartitionsCacheConstructor;
import ru.susu.algebra.partition.comparators.SymmByCharDegreeComparator;
import ru.susu.algebra.partition.filter.AllPartitionsFilter;
import ru.susu.algebra.properties.IPropertySource;
import ru.susu.algebra.properties.MapPropertySource;
import ru.susu.algebra.util.CollectionUtils;
import ru.susu.algebra.util.StringUtilities;

/**
 * @author akargapolov
 * @since: 09.03.2009
 */
public class StWorkspaceAllSymmetricGroupCharTableWriter
{
	private IPropertySource _ps;

	public StWorkspaceAllSymmetricGroupCharTableWriter()
	{
		this(new MapPropertySource());
	}

	public StWorkspaceAllSymmetricGroupCharTableWriter(IPropertySource ps)
	{
		_ps = ps;
	}

	public void write(Integer number) throws IOException, Exception
	{
		PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("ExecuteResults/SymmCharTables/" + number + ".txt")));

		List<Partition> partitions1 = new PartitionsCacheConstructor().getAscendingPartitions(number,
				new AllPartitionsFilter());
		List<Partition> partitions2 = new PartitionsCacheConstructor().getAscendingPartitions(number,
				new AllPartitionsFilter());

		SymmetricGroupMapTableConstructor constructor = SymmetricGroupMapTableConstructor.getInstance();

		partitions1 = CollectionUtils.sort(partitions1, true, new SymmByCharDegreeComparator());

		int width = 0;

		//Partition p1 = partitions.get(500);
		//Partition p2 = partitions.get(500);
		for (Partition p1 : partitions1)
			for (Partition p2 : partitions2)
			{
				int curWidth = constructor.getValue(p1, p2).toString().length();
				width = Math.max(width, curWidth + 1);
			}

		int index = 0;

		// head
		for (Partition p2 : partitions2)
		{
			int curWidth = index == 0 ? width * 2 + 1 : width;
			out.write(StringUtilities.toString(index++, curWidth));
		}
		out.write("\n");

		index = 0;
		for (Partition p1 : partitions1)
		{
			out.write(StringUtilities.toString(index++, width) + ":");
			for (Partition p2 : partitions2)
				out.write(StringUtilities.toString(constructor.getValue(p1, p2).toString(), width));
			out.print("\n");
		}
		out.close();
	}
}
